<?php
/**
 * created by zzy
 * date: 2018/1/26 14:07
 */


namespace App\Api\Service\Warehouse\Controllers;


use App\Eloquent\Ygt\OrderFieldCompany;
use App\Eloquent\Ygt\Warehouse;
use App\Eloquent\Ygt\WarehouseLog;
use App\Eloquent\Ygt\DepartmentUser;
use App\Engine\Func;
use Framework\BaseClass\Api\Controller;

class LogController extends Controller
{
    public $userId          = 25;
    public function index(){}

    public function getWaterList(){
        $userId             = Func::getHeaderValueByName( 'userid',$this->userId );
        $userInfo           = DepartmentUser::getCurrentInfo( $userId );

        if(!$userInfo)
        {
            xThrow(ERR_PARAMETER,'登录失败');
        }

        $companyId          = $userInfo->company_id;

        $date          = request('date','');
        $page          = request('page',1);


        //取材料的流水
        $limit          = 10;
        $offset         = ($page - 1) * $limit;

        $WarehouseObj = new WarehouseLog();

        $WarehouseObj = $WarehouseObj->where('ygt_warehouse_log.company_id','=',$companyId);
        if(!empty($date)){
            $startTime = strtotime($date);
            $endTime = $startTime+24*3600;

            $WarehouseObj = $WarehouseObj->whereBetween('ygt_warehouse_log.created_at',[$startTime,$endTime]);
        }
        
        //获得总数
        $count = $WarehouseObj->count();

        //获得数据
        $lists = $WarehouseObj->leftJoin('ygt_warehouse','ygt_warehouse.id','=','ygt_warehouse_log.warehouse_id')
            ->leftJoin('ygt_user','ygt_user.id','=','ygt_warehouse_log.user_id')
            ->skip($offset)->take($limit)
            ->orderBy('created_at','desc')
            ->get([
                'ygt_warehouse_log.id',
                'ygt_warehouse_log.type',
                'ygt_warehouse_log.sn',
                'ygt_warehouse_log.all_number as number',
                'ygt_warehouse_log.now_number as residual_number',
                'ygt_warehouse_log.created_at',
                'ygt_warehouse.product_name as name',
                'ygt_user.truename as operate_name'
            ])
            ->toArray();
        $typeList               = WarehouseLog::getTypeList();
        //修改时间显示格式
        foreach ($lists as $key => $tmpRow){
            $lists[$key]['created_time'] = date('Y-m-d H:i:s',$tmpRow['created_at']);
            $lists[$key]['created_at'] = date('Y-m-d H:i:s',$tmpRow['created_at']);

            $typeName           = '异常数据';
            $typeColor          = '#04C9B3';
            foreach($typeList as $k=>$v)
            {
                if($v['id'] == $tmpRow['type'])
                {
                    $typeName       = $v['title'];
                    $typeColor      = $v['color'];
                }
            }

            $lists[$key]['type_name'] = $typeName;
            $lists[$key]['type_color'] = $typeColor;
        }

        $listData           = [
            'list'=>$lists,
        ];
        $pagingData         = [
            'page' => $page,
            'pageSize' => $limit,
            'total' => $count,
            'keyword' => ''
        ];
        return $this->pagingData($listData, $pagingData);
    }


    //厂的成品列表
    public function lists()
    {
        $userId             = Func::getHeaderValueByName('userid',$this->userId);
        $userInfo           = DepartmentUser::getCurrentInfo($userId);
        $page               = request('page',1);
        $limit              = request('page_num',10);
        $warehouseId        = request('id',0);
        if(!$userInfo || !$warehouseId)
        {
            xThrow( ERR_PARAMETER );
        }
        $companyId          = $userInfo->company_id;
        $where              = ['id'=>$warehouseId];
        $info               = Warehouse::getInfo($where);
        if(!$info)
        {
            xThrow( ERR_UNKNOWN,'成品信息出错' );
        }
        $number             = $info->now_number;
        $numberIn           = $info->all_number;
        $numberOut          = $numberIn - $number;
        $numberOut          = $numberOut>0?$numberOut:-$numberOut;
        $info->number       = $number;
        $info->number_in    = $numberIn;
        $info->number_out   = $numberOut;
        $infoArr            = $info->toArray();
        $createdAt          = $infoArr['created_at'];
        $createdAtYmd       = date('Y-m-d',$createdAt);
        $infoArr['created_at_ymd']  = $createdAtYmd;
        //$specificationArr   = [['title'=>'','value'=>''],['title'=>'','value'=>'']];
        //SELECT * FROM ygt_order_field_company WHERE field_name='finished_specification'
        $where              = [
            'company_id'=>$companyId,
            'field_name'=>'finished_specification',
        ];
        $fieldInfo          = OrderFieldCompany::getInfo($where);
        $finishedSpecificationText  = '规格';
        if($fieldInfo)
        {
            $fieldType      = $fieldInfo->field_type;
            if($fieldType == 15){
                $finishedSpecificationText  = '宽*长';
            }else if($fieldType==16){
                $finishedSpecificationText  = '宽*长*M边';
            }
        }
        $infoArr['finished_specification_text']  = $finishedSpecificationText;


        //新增重量信息
        $infoArr['finished_weight_text']  = '成品重量';
        $tmpOrderId = $infoArr['order_id'];
        $tmpOrderInfo = \App\Eloquent\Ygt\Order::where(['id'=>$tmpOrderId])->first();

        $tmpFinishedWeight = '';
        if($tmpOrderInfo){
            $tmpFinishedWeight = $tmpOrderInfo['finished_weight'] ? $tmpOrderInfo['finished_weight'] : '';//成品重量
        }
        $infoArr['finished_weight']  = str_replace(',','',$tmpFinishedWeight);

        $infoImgPath        = $infoArr['img_path'];
        $imgPath            = \App\Engine\Func::getImgUrlHttp($infoImgPath);
        $infoArr['img_path']    = $imgPath;

        $offset             = ($page-1)*$limit;
        $where              = ['ygt_warehouse_log.warehouse_id'=>$warehouseId];
        $lists              = WarehouseLog::getWaterList($where,$limit,$offset)->toArray();

        //修改时间显示格式
        foreach ($lists as $key => $tmpRow){
            $lists[$key]['created_at'] = date('Y-m-d H:i:s',$tmpRow['created_at']);
            $lists[$key]['updated_at'] = date('Y-m-d H:i:s',$tmpRow['updated_at']);
        }

        $count              = WarehouseLog::getCount($where);
        $listData           = [
            'print_button'=>0,
            'info'=>$infoArr,
            'list'=>$lists,
        ];
        $pagingData         = [
            'page' => $page,
            'pageSize' => $limit,
            'total' => $count,
            'keyword' => ''
        ];
        return $this->pagingData($listData, $pagingData);
    }
}